package com.czl.base.data.source.impl

import com.czl.base.base.BaseBean
import com.czl.base.data.api.ApiService
import com.czl.base.data.bean.*
import com.czl.base.data.source.HttpDataSource
import com.google.gson.Gson
import io.reactivex.Observable
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

/**
 * @author Alwyn
 * @Date 2020/7/22
 * @Description
 */
class HttpDataImpl(private val apiService: ApiService) : HttpDataSource {

    override fun loginByPwd(paramsJson: String): Observable<BaseBean<UserBean>> {
        var body: RequestBody =
            RequestBody.create(MediaType.parse("application/json; charset=utf-8"), paramsJson)
        return apiService.loginByPwd(body)
    }

    override fun loginByPhoneCode(paramsJson: String): Observable<BaseBean<UserBean>> {
        var body: RequestBody =
            RequestBody.create(MediaType.parse("application/json; charset=utf-8"), paramsJson)
        return apiService.loginByPhoneCode(body)
    }

    override fun logout(): Observable<BaseBean<Any?>> {
        return apiService.logout()
    }

    override fun getHotLine(areaId: String): Observable<BaseBean<String>> {
        return apiService.getHotLine(areaId)
    }

    override fun getDeleteNotice(): Observable<BaseBean<String>> {
        return apiService.getDeleteNotice()
    }

    override fun getPhoneCode(phone: String): Observable<BaseBean<Any?>> {
        var body: RequestBody =
            toGsonParams(mapOf("phone" to phone, "templateCode" to "SMS_216373127"))
        return apiService.getPhoneCode(body)
    }

    override fun evaluateOrder(params: Map<String, String>): Observable<BaseBean<Any?>> {
        var body: RequestBody = toGsonParams(params)
        return apiService.evaluateOrder(body)
    }

    override fun evaluateComplaint(params: Map<String, String>): Observable<BaseBean<Any?>> {
        var body: RequestBody = toGsonParams(params)
        return apiService.evaluateComplaint(body)
    }

    override fun evaluateHouseKeeper(params: Map<String, String>): Observable<BaseBean<Any?>> {
        var body: RequestBody = toGsonParams(params)
        return apiService.evaluateHouseKeeper(body)
    }

    override fun getMonthPrice(vehicleNo: String, areaId: String): Observable<BaseBean<Float>> {
        return apiService.getMonthPrice(vehicleNo, areaId)
    }

    override fun parkCharging(vehicleNo: String): Observable<BaseBean<PayDetail>> {
        return apiService.parkCharging(vehicleNo)
    }

    override fun getUserRooms(phone: String, areaId: String): Observable<BaseBean<List<RoomBean>>> {
        return apiService.getUserRooms(phone, areaId)
    }

    override fun getApplyDetail(id: String): Observable<BaseBean<UserDetailBean>> {
        return apiService.getApplyDetail(id)
    }

    override fun getOwnerDetail(id: String): Observable<BaseBean<UserDetailBean>> {
        return apiService.getOwnerDetail(id)
    }

    override fun getReverseSearchCar(reverseSearchBean: ReverseCarRequestBean): Observable<BaseBean<ReverseSearchBean>> {
        var obj: String? = Gson().toJson(reverseSearchBean)
        var body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), obj)
        return apiService.reverseSearchCar(body)
    }

    override fun changeUserInfo(params: Map<String, String>): Observable<BaseBean<Any?>> {
        var body: RequestBody = toGsonParams(params)
        return apiService.changeUserInfo(body)
    }

    override fun uploadApplyFace(params: Map<String, String>): Observable<BaseBean<Any?>> {
        var body: RequestBody = toGsonParams(params)
        return apiService.uploadApplyFace(body)
    }

    override fun queryHouseKeeperList(params: Map<String, String>): Observable<BaseBean<HouseKeeperBean>> {
        var body: RequestBody = toGsonParams(params)
        return apiService.queryHouseKeeperList(body)
    }

    override fun getHouseKeepers(params: Map<String, String>): Observable<BaseBean<HouseKeeperListBean>> {
        var body:RequestBody=toGsonParams(params)
        return apiService.getHouseKeepers(body)
    }

    override fun webGetHouseKeepers(params: Map<String, String>): Observable<BaseBean<HouseKeeperListBean>> {
        val body:RequestBody=toGsonParams(params)
        return apiService.webGetHouseKeepers(body)
    }

    override fun getOrderType(pType: Int,areaNo: String): Observable<BaseBean<List<OrderType>>> {
        return apiService.getOrderType(pType,areaNo)
    }

    override fun submitReport(sub: SubmitReportRequestBean): Observable<BaseBean<Any>> {
        var body = RequestBody.create(
            MediaType.parse("application/json; charset=utf-8"),
            Gson().toJson(sub)
        )
        return apiService.submitReport(body)
    }

    override fun submitComplaint(com:ComplaintSubmitRequestBean): Observable<BaseBean<Any?>> {
        var body = RequestBody.create(
            MediaType.parse("application/json; charset=utf-8"),
            Gson().toJson(com)
        )
        return apiService.submitComplaint(body)
    }

    override fun getOrderDetail(no: String): Observable<BaseBean<OrderDetailBean>> {
        return apiService.getOrderDetail(no)
    }

    override fun getComplaintDetail(no: String): Observable<BaseBean<ComplaintDetailBean>> {
        return apiService.getComplaintDetail(no)
    }

    override fun getCommunities(communityRequestBean: CommunityRequestBean): Observable<BaseBean<CommunityResponseBean>> {
        var body = RequestBody.create(
            MediaType.parse("application/json; charset=utf-8"),
            Gson().toJson(communityRequestBean)
        )
        return apiService.getCommunities(body)
    }

    override fun getNotices(communityRequestBean: CommunityRequestBean): Observable<BaseBean<NoticeResponseBean>> {
        var body = RequestBody.create(
            MediaType.parse("application/json; charset=utf-8"),
            Gson().toJson(communityRequestBean)
        )
        return apiService.getNotices(body)
    }

    override fun getActivityProfileList(params: Map<String, String>): Observable<BaseBean<ActivityProfileListBean>> {
        val body=toGsonParams(params)
        return apiService.getActivityProfileList(body)
    }

    override fun getActivityDetail(activityId: String): Observable<BaseBean<ActivityDetailBean>> {
        return apiService.getActivityDetail(activityId)
    }

    override fun joinActivity(activityId: String): Observable<BaseBean<Any?>> {
        return apiService.joinActivity(activityId)
    }

    override fun getBulletinDetail(id: Int): Observable<BaseBean<BulletinDetailBean>> {
        return apiService.getBulletinDetail(id)
    }


    override fun retrievePassword(
        phone: String,
        code: String,
        pwd: String
    ): Observable<BaseBean<Any?>> {
        var body: RequestBody =
            toGsonParams(mapOf("phone" to phone, "verifyCode" to code, "newPassword" to pwd))
        return apiService.retrievePassword(body)
    }

    override fun deleteCarList(
        vehicleNos: List<String>,
        areaId: String
    ): Observable<BaseBean<Any?>> {
        val obj: String = Gson().toJson(mapOf("vehicleNos" to vehicleNos, "areaId" to areaId))
        val body: RequestBody =
            RequestBody.create(MediaType.parse("application/json; charset=utf-8"), obj)
        return apiService.deleteCarList(body)
    }

    override fun deleteQueryCar(vehicleNo: String, areaId: String): Observable<BaseBean<Any?>> {
        return apiService.deleteQueryCar(vehicleNo, areaId)
    }

    override fun getMyQueryHistory(): Observable<BaseBean<List<CarItem>>> {
        return apiService.getMyQueryHistory()
    }

    override fun getMyCarList(
        vehiclesWithNoPlan: Boolean,
        areaId: String
    ): Observable<BaseBean<List<CarItem>>> {
        return apiService.getMyCarList(vehiclesWithNoPlan, areaId)
    }

    override fun getRepairList(
        params: Map<String, String>
    ): Observable<BaseBean<ReportListBean>> {
        var body: RequestBody = toGsonParams(params)
        return apiService.getRepairList(body)
    }

    override fun getNoticeMsgList(params: Map<String, String>): Observable<BaseBean<NoticeListBean>> {
        var body: RequestBody = toGsonParams(params)
        return apiService.getNoticeMsgList(body)
    }

    override fun getComplaintList(params: Map<String, String>): Observable<BaseBean<ReportListBean>> {
        var body:RequestBody = toGsonParams(params)
        return apiService.getComplainList(body)
    }

    override fun getScoreList(params: Map<String, String>): Observable<BaseBean<ScoreListBean>> {
        var body: RequestBody = toGsonParams(params)
        return apiService.getScoreList(body)
    }

    override fun addPeople(
        params: Map<String, String>
    ): Observable<BaseBean<Any?>> {
        var body: RequestBody = toGsonParams(params)
        return apiService.addPeople(body)
    }

    override fun updatePeople(params: Map<String, String>): Observable<BaseBean<Any?>> {
        var body: RequestBody = toGsonParams(params)
        return apiService.updatePeople(body)
    }

    override fun getApplyRecords(areaId: String): Observable<BaseBean<List<AddRecordBean>>> {
        return apiService.getApplyRecords(areaId)
    }

    override fun deleteApply(id: String): Observable<BaseBean<Any?>> {
        return apiService.deleteApply(id)
    }

    override fun deleteHouseUser(id: String): Observable<BaseBean<Any?>> {
        return apiService.deleteHouseUser(id)
    }

    override fun getOwnerRecords(areaId: String,houseId:String): Observable<BaseBean<List<PeopleManagerBean>>> {
        return apiService.getOwnerRecords(areaId,houseId)
    }

    override fun getPointDetail(areaNo: String): Observable<BaseBean<String>> {
        return apiService.getPointDetail(areaNo)
    }

    override fun getIntegralList(
        params: Map<String, String>
    ): Observable<BaseBean<IntegralBean>> {
        var body: RequestBody = toGsonParams(params)
        return apiService.getIntegralList(body)
    }


    override fun verifyPwdNet(
        newPassword: String,
        oldPassword: String
    ): Observable<BaseBean<Any?>> {
        var body: RequestBody = toGsonParams(
            mapOf(
                "newPassword" to newPassword,
                "oldPassword" to oldPassword
            )
        )

        return apiService.verifyPwdNet(body)
    }

    override fun initPassword(newPassword: String): Observable<BaseBean<Any?>> {
        var body: RequestBody = toGsonParams(
            mapOf(
                "password" to newPassword
            )
        )
        return apiService.initPassword(body)
    }

    private fun toGsonParams(params: Map<String, String>): RequestBody {
        var gson = Gson()
        var obj: String = gson.toJson(
            params
        )
        return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), obj)
    }

    override fun deleteUserAccount(): Observable<BaseBean<Any?>> {
        return apiService.deleteUserAccount()
    }

    override fun cancelWorkOrder(no: String): Observable<BaseBean<Any?>> {
        return apiService.cancelWorkOrder(no)
    }

    override fun readNotice(msgId: String): Observable<BaseBean<Any?>> {
        return apiService.readNotice(msgId)
    }

    override fun deleteWorkOrder(no: String): Observable<BaseBean<Any?>> {
        return apiService.deleteWorkOrder(no)
    }

    override fun cancelComplaint(no: String): Observable<BaseBean<Any?>> {
        return apiService.cancelComplaint(no)
    }

    override fun deleteComplaint(no: String): Observable<BaseBean<Any?>> {
        return apiService.deleteComplaint(no)
    }

    override fun addPersonCar(params: Map<String, String>): Observable<BaseBean<Any?>> {
        var body: RequestBody = toGsonParams(params)
        return apiService.addPersonCar(body)
    }

    override fun register(
        username: String,
        password: String,
        repassword: String
    ): Observable<BaseBean<Any?>> {
        return apiService.register(username, password, repassword)
    }

    override fun uploadHeadImg(imgSrc: String): Observable<BaseBean<ImgRspBean>> {
        var file = File(imgSrc)
        // 创建 RequestBody，用于封装构建RequestBody
//        var requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file)
        var requestFile = RequestBody.create(MediaType.parse("image/jpg"), file);
        // MultipartBody.Part  和后端约定好Key，这里的partName是用file
        var body = MultipartBody.Part.createFormData("file", file.name, requestFile)
        return apiService.uploadHeadImg(body)
    }

    override fun uploadHeadImg(
        bucketName: String,
        imgSrc: String,
        folderName: String
    ): Observable<BaseBean<ImgRspBean>> {
        var file = File(imgSrc)
        // 创建 RequestBody，用于封装构建RequestBody
//        var requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file)
        var requestFile:RequestBody
        if (imgSrc.toLowerCase().endsWith(".mp4")){
            requestFile = RequestBody.create(MediaType.parse("video/mp4"), file);
        }else{
            requestFile = RequestBody.create(MediaType.parse("image/jpg"), file);
        }
        // MultipartBody.Part  和后端约定好Key，这里的partName是用file
        var body = MultipartBody.Part.createFormData("file", file.name, requestFile)
        return apiService.uploadHeadImg(bucketName,body,folderName)
    }

    override fun getRecordDetail(orderNo: String): Observable<BaseBean<RecordDetailBean>> {
        return apiService.getRecordDetail(orderNo)
    }

    override fun queryPayResult(
        orderNo: String,
        areaId: String
    ): Observable<BaseBean<PayResultBean>> {
        return apiService.queryPayResult(orderNo, areaId)
    }

    override fun getMonthCardList(
        pageNum: Int,
        pageSize: Int,
        areaId: String
    ): Observable<BaseBean<MonthCardListBean>> {
        var pageParams =
            PageParams(pageNum.toString(), pageSize.toString(), "", PageParams.Data(areaId))
        var body: RequestBody =
            RequestBody.create(
                MediaType.parse("application/json; charset=utf-8"),
                Gson().toJson(pageParams)
            )
        return apiService.getMonthCardList(body)
    }

    override fun getPayRecordList(
        params: Map<String, String>
    ): Observable<BaseBean<PayRecordListBean>> {
        var body: RequestBody = toGsonParams(params)
        return apiService.getPayRecordList(body)
    }

    override fun placeAnOrder(params: Map<String, String>): Observable<BaseBean<PayInfoBean>> {
        var body: RequestBody = toGsonParams(params)
        return apiService.placeAnOrder(body)
    }

    override fun payWorkOrder(params: Map<String, String>): Observable<BaseBean<WorkOrderPayBean>> {
        var body: RequestBody = toGsonParams(params)
        return apiService.payWorkOrder(body)
    }

    override fun getVersionName(): Observable<BaseBean<Any?>> {
        return apiService.getVersionName("0")
    }

    override fun getUserInfoNet(): Observable<BaseBean<UserInfo>> {
        return apiService.getUserInfoNet()
    }

    override fun getApplyDetails(applyId: String): Observable<BaseBean<ApplyDetailBean>> {
        return apiService.getApplyDetails(applyId)
    }

    override fun applyAudit(
        applyId: String,
        approveRemark: String,
        approveStatus: String
    ): Observable<BaseBean<String>> {
        val params =
            ApplyParams(
                applyId = applyId,
                approveRemark = approveRemark,
                approveStatus = approveStatus
            )
        val body: RequestBody =
            RequestBody.create(
                MediaType.parse("application/json; charset=utf-8"),
                Gson().toJson(params)
            )
        return apiService.applyAudit(body)
    }

    override fun getApplyList(
        pageNum: Int,
        pageSize: Int,
        filter: String
    ): Observable<BaseBean<ApplyBean>> {
        val pageParams =
            ApplyPageParams(
                pageNum.toString(),
                pageSize.toString(),
                "",
                ApplyPageParams.Data(state = filter)
            )
        val body: RequestBody =
            RequestBody.create(
                MediaType.parse("application/json; charset=utf-8"),
                Gson().toJson(pageParams)
            )
        return apiService.getApplyList(body)
    }

    override fun getApplyResult(
        applyId: String,
        mobile: String
    ): Observable<BaseBean<ApplyResultBean>> {
        return apiService.getApplyResult(applyId, mobile)
    }

    override fun getTakeCareFullOrder(orderId: String): Observable<BaseBean<WorkOrderDetailBean>> {
        return apiService.getTakeCareFullOrder(orderId)
    }

    override fun listBuilding(): Observable<BaseBean<List<BuildingtBean>>> {
        return apiService.listBuilding()
    }

    override fun listFloor(
        buildingId: String
    ): Observable<BaseBean<List<FloorBean>>> {
        return apiService.listFloor(buildingId)
    }

    override fun listPointType(
        systemId: String
    ): Observable<BaseBean<List<DeviceTypeBean>>> {
        return apiService.listPointType(systemId)
    }

    override fun takeCareOrderRecord(
        pageNum: Int,
        pageSize: Int,
        filter: TakeCareRecordFilterParams
    ): Observable<BaseBean<TakeCareRecordBean>> {
        val pageParams =
            TakeCarePageAllParams(
                pageNum.toString(),
                pageSize.toString(),
                "",
                filter
            )
        val body: RequestBody =
            RequestBody.create(
                MediaType.parse("application/json; charset=utf-8"),
                Gson().toJson(pageParams)
            )
        return apiService.inspectTaskTaskPage(body)
    }

    override fun addApply(params: GuestApplyParam): Observable<BaseBean<String>> {
        val body: RequestBody =
            RequestBody.create(
                MediaType.parse("application/json; charset=utf-8"),
                Gson().toJson(params)
            )
        return apiService.addApply(body)
    }

    override fun uploadImg(imgSrc: String): Observable<BaseBean<ImgRspBean>> {
        var file = File(imgSrc)
        // 创建 RequestBody，用于封装构建RequestBody
//        var requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file)
        var requestFile = RequestBody.create(MediaType.parse("image/jpg"), file);
        // MultipartBody.Part  和后端约定好Key，这里的partName是用file
        var body = MultipartBody.Part.createFormData("file", file.name, requestFile)
        val params = mapOf<String, String>("bucketName" to "guest", "folderName" to "")
        return apiService.uploadImg(params,body)
    }

    override fun takeCareOrderScanRecord(
        pageNum: Int,
        pageSize: Int,
        filter: String
    ): Observable<BaseBean<TakeCareRecordBean>> {
        val pageParams =
            TakeCareRecordPageParams(
                pageNum.toString(),
                pageSize.toString(),
                "",
                filter
            )
        val body: RequestBody =
            RequestBody.create(
                MediaType.parse("application/json; charset=utf-8"),
                Gson().toJson(pageParams)
            )
        return apiService.inspectTaskTaskScanPage(body)
    }
}